package com.zsoft.webmail.servlet.hainannet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.zsoft.webmail.Application;
import comm.util.Utils;
import com.zsoft.webmail.message.Header;
import com.zsoft.webmail.*;
import com.zsoft.webmail.folder.*;

public class FolderClearServlet extends HttpServlet {
  //private static final String CONTENT_TYPE = Application.getProperty("ContentType");
  private static final String CONTENT_TYPE = "text/html";
    /**Initialize global variables*/
    public void init() throws ServletException {
    }
    /**Process the HTTP Get request*/
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setContentType(CONTENT_TYPE);
        //PrintWriter out = response.getWriter();
        comm.http.Request request1=new comm.http.Request(request,Application.getProperty("defaultInputCharset"));
        MemberAuthorization memberAuthToken=null ;
        HttpSession session=request.getSession();
        Object object = session.getAttribute("authBean");
        if(object==null){
          session.setAttribute("authBean",new com.zsoft.webmail.AuthBean(session.getId(),request.getRemoteAddr()));
          response.sendRedirect( "login.jsp" );
          return;
        }else{
          memberAuthToken = ((AuthBean)object).getMemberAuthToken();
          if(memberAuthToken==null){
            response.sendRedirect( "login.jsp" );
            return;
          }
        }

        String folderId=request1.getParameter("folderId");
        String returnPath=request1.getParameter("returnPath");
        if(returnPath==null)returnPath="FolerList";
        folderId=comm.util.FileUtils.getFilename(folderId);


        Folder mybox=new Folder(memberAuthToken,folderId);

        Vector mails = mybox.listHeaders();
        Vector vector = new Vector();
        for(int i=0; i<mails.size(); i++)
        {

              Header header =  (com.zsoft.webmail.message.Header)(mails.get(i)) ;
              String mid= header.getValue("MessageName");
              //out.println(mid);
              mid=comm.util.FileUtils.getFilename(mid);
              if(folderId.equals("garbage")){
                try{
                  mybox.deleteMessage(mid);
                }catch(Exception e){}
              }else{
                  vector.add(mid);
              }
              header=null;
              mid=null;

        }


        if(!folderId.equals("garbage"))
        {
            vector.trimToSize();
            String[] msgs=new String[vector.size()];
            vector.copyInto(msgs);
            try{
                  mybox.moveMessages(msgs,"garbage");
            }catch(Exception e){}
        }
        response.sendRedirect(returnPath);


    }

    /**Process the HTTP Post request*/
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doGet(request,response);
    }
    /**Clean up resources*/
    public void destroy() {
    }
}